import {
  Controller,
  Post,
  Get,
  Patch,
  Delete,
  Param,
  Body,
  ParseIntPipe,
} from '@nestjs/common';
import { RoleService } from './role.service';
import { ObjectType, CommonResponse } from 'src/types';
import { responseWrapper } from 'src/utils';

@Controller('role')
export class RoleController {
  constructor(private readonly roleService: RoleService) {}

  @Post()
  async createRole(@Body() data: ObjectType): Promise<CommonResponse> {
    try {
      const res = await this.roleService.createRole(data);
      return responseWrapper(res);
    } catch (error) {
      return responseWrapper(error, true);
    }
  }

  @Delete(':id')
  async deleteRoleById(
    @Param('id', new ParseIntPipe()) id: number, // 接口中使用pip
  ): Promise<CommonResponse> {
    try {
      const res = await this.roleService.deleteRoleById(id);
      return responseWrapper(res);
    } catch (error) {
      return responseWrapper(error, true);
    }
  }

  @Patch(':id')
  async modifyRoleById(
    @Param('id', new ParseIntPipe()) id: number,
    @Body() data: ObjectType,
  ): Promise<CommonResponse> {
    try {
      const res = await this.roleService.modifyRoleById(id, data);
      return responseWrapper(res);
    } catch (error) {
      return responseWrapper(error, true);
    }
  }

  @Get()
  async roleList(): Promise<CommonResponse> {
    try {
      const res = await this.roleService.roleList();
      return responseWrapper(res);
    } catch (error) {
      return responseWrapper(error, true);
    }
  }

  @Get(':id')
  async getRoleById(
    @Param('id', new ParseIntPipe()) id: number,
  ): Promise<CommonResponse> {
    try {
      const res = await this.roleService.roleById(id);
      return responseWrapper(res);
    } catch (error) {
      return responseWrapper(error, true);
    }
  }
}
